package com.cmes.oauth.web.controller;

import com.cmes.core.context.security.SecurityContextHolder;
import com.cmes.core.domain.bo.IdBO;
import com.cmes.oauth.core.service.LoginService;
import com.cmes.security.StpAdminUtil;
import com.cmes.system.api.bo.UsernamePasswordBO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Map;

/**
 * 部门管理
 *
 * @author Bruce.Gong
 * @since 1.0.0-SNAPSHOT
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/oauth")
public class AuthController {

    private final LoginService loginService;

    @PostMapping("login")
    public String login(@RequestBody @Valid UsernamePasswordBO bo) {
        return loginService.doLogin(bo.getUsername(), bo.getPassword());
    }

    @PostMapping("checkLogin")
    public Map<String, Object> checkLogin() {
        Long userId = SecurityContextHolder.getCurrentUserId();
        return loginService.loginInfo(userId);
    }

    @PostMapping("logout")
    public void logout() {
        loginService.logout(SecurityContextHolder.getCurrentUserId());
    }

    @PostMapping("kickOut")
    public void kickOut(@RequestBody @Valid IdBO bo) {
        StpAdminUtil.kickout(bo.getId());
    }

}
